//
//  ZYCEducationGrowthFileViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/18.
//

#import "ZYCEducationGrowthFileViewController.h"
#import "PullDownMenuView.h"
#import "ZYCGrowthFileCell.h"
#import "ZYCPullDownMenuViewController.h"
#import "ZYCCommonBaseModel.h"
#import "ZYCGrowthFileModel.h"
#import "ZYCCircleFriendsViewController.h"
@interface ZYCEducationGrowthFileViewController ()<UITableViewDelegate,UITableViewDataSource,PullDownMenuViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) PullDownMenuView *menuView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSArray *testArr;
@property (nonatomic, strong) NSArray *test1Arr;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *viewControlls;
@property (nonatomic, copy) NSString *grade;
@property (nonatomic, copy) NSString *gradeChinese;
@property (nonatomic, copy) NSString *classId;
@property (nonatomic, copy) NSString *classChinese;
@end

@implementation ZYCEducationGrowthFileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"成长档案";
    ZYCCommonBaseModel *model = self.testArr[1];
    self.grade = model.nameId;
    self.gradeChinese = model.name;

    [self getClassListByGrade:self.grade];
    
    
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.tableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealWithNot:) name:UpdateMenuTitleNote object:nil];
}

- (void)dealWithNot:(NSNotification *)note {
    if (![self.viewControlls containsObject:note.object]) {
        return;
    }
    NSInteger col = [self.viewControlls indexOfObject:note.object];
    NSArray *allValues = note.userInfo.allValues;
//    // 不需要设置标题,字典个数大于1，或者有数组
    if (allValues.count > 1 || [allValues.firstObject isKindOfClass:[NSArray class]]) return ;
    if (col == 0) {
        ZYCCommonBaseModel *model = allValues.firstObject;
        self.grade = model.nameId;
        self.gradeChinese = model.name;
        self.titleArr = @[self.gradeChinese,@"班级"];
        [self getClassListByGrade:self.grade];
    }else if(col == 1){
        ZYCCommonBaseModel *model = allValues.firstObject;
        self.classId = model.nameId;
        [self queryStuByClassId:self.classId];
    }
    
}


#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCGrowthFileCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ ZYCGrowthFileCell class]) forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZYCCircleFriendsViewController *vc = [[ZYCCircleFriendsViewController alloc] init];
    ZYCGrowthFileModel *model = self.dataSource[indexPath.row];
    vc.titleString = model.name;
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - PullDownMenuViewDataSource
// 返回下拉菜单每列按钮标题
- (NSString *)pullDownMenu:(PullDownMenuView *)pullDownMenu titleForColAtIndex:(NSInteger)index {
    return [self.titleArr objectAtIndex:index];
}

// 返回下拉菜单每列对应的控制器
- (UIViewController *)pullDownMenu:(PullDownMenuView *)pullDownMenu viewControllerForColAtIndex:(NSInteger)index {
    ZYCPullDownMenuViewController *v = [self.viewControlls objectAtIndex:index];
    if (index == 0) {
        v.array = self.testArr.mutableCopy;

    }else{
        v.array = self.test1Arr.mutableCopy;

    }
    return v;
}

// 返回下拉菜单多少列
- (NSInteger)numberOfColsInMenu:(PullDownMenuView *)pullDownMenu {
    return self.viewControlls.count;
}
- (CGFloat)pullDownMenu:(PullDownMenuView *)pullDownMenu heightForColAtIndex:(NSInteger)index {
    if (index == 0) {
        return self.testArr.count * 44;
    }else{
        return self.test1Arr.count * 44;
    }
}


- (NSArray *)viewControlls {
    if (!_viewControlls) {
        _viewControlls = @[[NSClassFromString(@"ZYCPullDownMenuViewController") new],[NSClassFromString(@"ZYCPullDownMenuViewController") new]];
    }
    return _viewControlls;
}


- (void)queryStuByClassId:(NSString *)classId{
    NSDictionary *params = @{
        @"classId":classId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GrowthQueryStuList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {

        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                [self.dataSource removeAllObjects];
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCGrowthFileModel class] json:responseObject[@"body"]];
                [self.dataSource addObjectsFromArray:array];
                [self.tableView reloadData];
            }
        } failure:^(NSError * _Nonnull error) {

        }];
}

- (NSArray *)testArr {
    if (!_testArr) {
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [documentPath stringByAppendingPathComponent:@"grade.data"];
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        _testArr = array;
    }
    return _testArr;
}

- (void)getClassListByGrade:(NSString *)grade{
    NSDictionary *params = @{
        @"grade":grade
    };

    [[URLManager sharedURLManager] getUrlString:Common_Post_QueryClassListByGrade requestService:RequestServiceWorkBench parameters:params progress:nil success:^(id  _Nullable responseObject) {
        if (SUCCESS(responseObject[@"code"])) {
            NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCCommonBaseModel class] json:responseObject[@"body"]];
            ZYCCommonBaseModel *first =array.firstObject;
            self.classChinese = first.name;
            self.test1Arr = array;
            self.titleArr = @[self.gradeChinese,self.classChinese];
            [self queryStuByClassId:first.nameId];
            self.menuView.dataSource = self;
   
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
    
}



- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.menuView.frame), kScreenWidth, kScreenHeight - CGRectGetMaxY(self.menuView.frame)) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCGrowthFileCell class] forCellReuseIdentifier:NSStringFromClass([ZYCGrowthFileCell class])];
        _tableView.showsVerticalScrollIndicator = NO;

    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

- (PullDownMenuView *)menuView {
    if (!_menuView) {
        _menuView = [[PullDownMenuView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 46.0f)];
        _menuView.separateLineColor = [UIColor clearColor];
        _menuView.titleFont = kCustomFont(@"PingFangSC-Regular", 15.0f);
    }
    return _menuView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
